package com.pj.xfxh.project.controller;

import com.pj.xfxh.project.entity.TopicMessage;
import com.pj.xfxh.project.service.TopicMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/topic/message")
public class TopicMessageController {

    @Autowired
    private TopicMessageService topicMessageService;

    @GetMapping("/messageId/{messageId}")
    public ResponseEntity<TopicMessage> getMessageById(@PathVariable int messageId) {
        TopicMessage message = topicMessageService.getById(messageId);
        return ResponseEntity.ok (message);
    }

    @GetMapping("/topicId/{topicId}")
    public ResponseEntity<List<TopicMessage>> getMessagesByTopicId(@PathVariable int topicId) {
        List<TopicMessage> messages = topicMessageService.listByMap(Collections.singletonMap("topic_id", topicId));
        return ResponseEntity.ok(messages);
    }

    @PostMapping("/{topicId}")
    public ResponseEntity<String> addMessage(@PathVariable Long topicId, @RequestBody TopicMessage message) {
        // 在实际应用中，可能需要设置消息的话题ID等信息
        message.setTopicId(topicId);
        topicMessageService.save(message);
        return ResponseEntity.ok("Message added successfully");
    }
}